//
// Created by wenyaxi on 24-10-29.
//
#include <base.h>
#include <ListNode.h>
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* dummy = new ListNode(0);
        ListNode* p = dummy;
        while (list1 != nullptr && list2 != nullptr) {
            if (list1->val < list2->val) {
                p->next = list1;
                p = p->next;
                list1 = list1->next;
            }else {
                p->next = list2;
                p = p->next;
                list2 = list2->next;
            }
        }
        while (list1 != nullptr) {
            p->next = list1;
            p = p->next;
            list1 = list1->next;
        }
        while (list2 != nullptr) {
            p->next = list2;
            p = p->next;
            list2 = list2->next;
        }
        return dummy->next;
    }
};